package org.android.com;

import org.android.com.R.string;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class CountDownTest_1 extends Activity {
		
	private final static String MY_PREFERENCES = "MyPref";
	private final static String TEXT_DATA_KEY = "textData";
	
	TextView timeDisplay;
	MyCount counter;
	int state = 0;
	int length = 200000;
	int length1 = 0;
	long startTime = 0;
	long currentTime = 0;
	long timeElapsed = 0;
	long timeRemaining = 0;
	long prevTimeRemaining = 0;
	Button control;
	Button stop;
	private MediaPlayer mMediaPlayer;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
    	//savedInstanceState
        super.onCreate(icicle);
        setContentView(R.layout.main);
                
        overridePendingTransition(R.anim.mainfadein,R.anim.splashfadeout);
        
        Intent i = getIntent();
        String pkg = getPackageName();
        int millis1 = i.getIntExtra(pkg+".myMillis", -1);
        Log.v("Log Tag", "millis1: " + millis1);
        if (millis1 == -1) {
        	//length = 0;
        	updatePreferencesData();
		} else {
			length = millis1;
		}
        //length1 = extras.getInt(Setting.millis);
        Log.v("Log Tag", "millis2: " + length);
        
        timeDisplay = (TextView) findViewById(R.id.timer);
        timeDisplay.setText(formatTime(length));
        control = (Button) findViewById(R.id.control);
        stop = (Button) findViewById(R.id.stop);
        counter = new MyCount(length,100);
        
        
    }
    
    public void control(View view) {
		
    	switch (state) {
		case 0:
			startTime = System.currentTimeMillis();
			counter.start();
			control.setText(R.string.pause);
			state = 1;
			break;
		case 1:
			//pause
			currentTime = System.currentTimeMillis();
			timeElapsed = currentTime - startTime;
			if (prevTimeRemaining == 0)
				timeRemaining = length - timeElapsed;
			else
				timeRemaining = prevTimeRemaining - timeElapsed;
			counter.cancel();
			//timeDisplay.setText(R.string.resume);
			control.setText(R.string.resume);
			timeDisplay.setText(formatTime(timeRemaining));
			prevTimeRemaining = timeRemaining;
			
			//resume
			counter = new MyCount(timeRemaining, 100);
			state = 0;
			break;
		case 2:
			prevTimeRemaining = 0;
			counter = new MyCount(length, 100);
			control.setText(R.string.start);
			timeDisplay.setText(formatTime(length));
			state = 0;
		default:
			break;
		}

	}
    public class MyCount extends CountDownTimer {
    	
    	public MyCount(long millisInFuture, long countDownInterval){
    		super(millisInFuture, countDownInterval);
    	}
    	
    	public void onFinish() {
    		playAudio1();
			timeDisplay.setText("Done!");
			state = 2;
			control.setText(R.string.restart);
		}
    	
    	public void onTick(long millisUntilFinished) {
			//timeDisplay.setText("Left: " + millisUntilFinished/100);
    		timeDisplay.setText(formatTime(millisUntilFinished));
		}
    }
    public void stop (View view){
    	timeDisplay.setText("Done!");
    	state = 2;
    	control.setText(R.string.restart);
    	counter.cancel();
    }
    public void setting (View view){
    	Intent i = new Intent(view.getContext(),Setting.class);
    	view.getContext().startActivity(i);
    }
    
    public String formatTime (long millis){
    	String output = "00:00:00";
    	long seconds = millis / 1000;
    	long minutes = seconds / 60;
    	long hours = minutes / 60;
    	
    	seconds = seconds % 60;
    	minutes = minutes % 60;
    	hours = hours % 60;
    	
    	String secondsD = String.valueOf(seconds);
    	String minutesD = String.valueOf(minutes);
    	String hoursD = String.valueOf(hours);
    	
    	if (seconds < 10)
    		secondsD = "0" + seconds;
    	if (minutes < 10)
    		minutesD = "0" + minutes;
    	if (hours < 10)
    		hoursD = "0" + hours;
    	
    	output = hoursD + " : " + minutesD + " : " + secondsD;
    	return output;
    }
    
    public void playAudio() {
    	try {
    		// http://www.soundjay.com/beep-sounds-1.html lots of free beeps here
    		mMediaPlayer = MediaPlayer.create(this,R.raw.beep9);
    		mMediaPlayer.setLooping(false);
    		Log.e("beep","started0");
    		mMediaPlayer.start();
    		mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
				
				public void onCompletion(MediaPlayer arg0) {
					// TODO Auto-generated method stub
					finish();
				}
			});
			
		} catch (Exception e) {
			// TODO: handle exception
			Log.e("beep","error:" + e.getMessage(),e);
		}
    }
    
    public void playAudio1() {
    	
    		// http://www.soundjay.com/beep-sounds-1.html lots of free beeps here
    		mMediaPlayer = MediaPlayer.create(this,R.raw.beep9);
    		mMediaPlayer.setLooping(false);
    		Log.e("beep","started0");
    		mMediaPlayer.start();
    		mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
				
				public void onCompletion(MediaPlayer arg0) {
					// TODO Auto-generated method stub
					finish();
				}
			});
			
	    }
    
    private void updatePreferencesData(){
 
        SharedPreferences prefs = getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE);
        
        int textData = Integer.parseInt(prefs.getString(TEXT_DATA_KEY, "0"));
        
        length = textData;
}
    
    @Override
    protected void onDestroy(){
    	super.onDestroy();
    	if (mMediaPlayer != null){
    		mMediaPlayer.release();
    		mMediaPlayer = null;
    	}
    }
}